package tools;

import InterfaceMahasiswa.FormPresensi;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.List;
import javax.swing.Timer;
import de.humatic.dsj.*;
import de.humatic.dsj.rc.RendererControls;
import javax.swing.JOptionPane;
import tools.ZBar.Symbol;

public class QRReader implements java.beans.PropertyChangeListener, ActionListener {

    private DSCapture graph;
    private String kode;
    private String Mkode;

    public QRReader() {
    }

    public void createGraph() {

        javax.swing.JFrame f = new javax.swing.JFrame("qr");
        f.setLocation(610, 0);
        f.setSize(400, 400);

        DSFilterInfo[][] dsi = DSCapture.queryDevices();

        graph = new DSCapture(DSFiltergraph.DD7, dsi[0][0], false, DSFilterInfo.doNotRender(), this);

        f.add(java.awt.BorderLayout.CENTER, graph.asComponent());

        f.pack();

        f.setVisible(true);

        f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Timer timer = new Timer(300, this);
        timer.start();
    }

    public void propertyChange(java.beans.PropertyChangeEvent pe) {
        switch (DSJUtils.getEventType(pe)) {
        }
    }

    public static void main(String[] args) {
        new QRReader().createGraph();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        BufferedImage bi = graph.getImage();
        List<ZBar.Symbol> ls = ZBar.scan(bi);
        if (ls.size() <= 0) {
            return;
        }

        BufferedImage overlay = new BufferedImage(graph.getWidth(), graph.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = overlay.createGraphics();
        g2d.setBackground(new Color(0, 0, 0, 0));
        g2d.setColor(Color.GREEN);
        int y0 = 0;

        for (ZBar.Symbol s : ls) {
            if (!kode.matches(s.data)) {
                System.out.println(s.data);
//                Kodes);
//
//                setMkode(getKode());
                g2d.drawPolygon(s.xs, s.ys, s.xs.length);
                g2d.drawString(s.data, 0, y0 += 20);

            } else if (kode.equals(s)) {
                JOptionPane.showMessageDialog(graph, "Anda Sudah Login");
            }
        }

        g2d.dispose();

        RendererControls rc = graph.getRendererControls();
        rc.setOverlayImage(overlay, null, Color.black, 1);
    }

//    public String showNiM() {
//        String NIM = getMkode();
//        return NIM;
//    }

//    /**
//     * @return the kode
//     */
//    public Symbol getKode() {
//        return kode;
//    }
//
//    /**
//     * @param kode the kode to set
//     */
//    public void setKode(Symbol kode) {
//        this.kode = kode;
//    }
//
//    /**
//     * @return the Mkode
//     */
//    public Symbol getMkode() {
//        return Mkode;
//    }
//
//    /**
//     * @param Mkode the Mkode to set
//     */
//    public void setMkode(Symbol Mkode) {
//        this.Mkode = Mkode;
//    }

    
}
